//
//  ALPEditorDetailVC.m
//  AlpFM
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 Alp. All rights reserved.
//

#import "ALPEditorDetailVC.h"
#import "ALPEditorDetailCell.h"
#import "ALPEditorDetailModel.h"
#import "ALPEditorPlayVC.h"  //播放列表页
#import "MJRefresh.h"
@interface ALPEditorDetailVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *allarray;
@property (nonatomic, assign)NSInteger nextPage;//记录下一页
@property (nonatomic, assign)BOOL isUpLoading;//记录上拉还是下拉 上拉为yes
@end

@implementation ALPEditorDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.alpha = 0.5;
    
    UIImage *image3 = [UIImage imageNamed:@"fanhuijiantou1"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image3 style:UIBarButtonItemStylePlain target:self action:@selector(fanhui:)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.strTitle;
    
    self.allarray = [NSMutableArray array];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 600) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消分割线
    [self.view addSubview:self.myTableView];
  
    [self.myTableView registerClass:[ALPEditorDetailCell class] forCellReuseIdentifier:@"cell"];

    [self addHeader];
    [self addFooter];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 86;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allarray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ALPEditorDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.mol = self.allarray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];  //取消选中状态
    ALPEditorPlayVC *VC = [[ALPEditorPlayVC alloc]init];
    VC.indice = indexPath.row + 1;
    ALPEditorDetailModel *mol = [self.allarray objectAtIndex:indexPath.row];
    VC.idid = mol.albumId;
    VC.jishu = mol.tracksCounts;
    VC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:VC animated:YES completion:^{
        
        
    }];
}


-(void)fanhui:(UIBarButtonItem *)btn{
    [self.navigationController popToRootViewControllerAnimated:YES];
}



#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block ALPEditorDetailVC *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:vc.nextPage];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
    
}
#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ALPEditorDetailVC *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.nextPage];//请求数据
        
    }];
}

-(void)GetPersonList:(NSInteger)nextPage{
    NSString *strUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=android&pageId=%ld&pageSize=20&title=更多",nextPage];
    [ALPAppTools getWithAFNurl:strUrl block:^(id result) {
        
        if (self.isUpLoading == NO) {
            [self.allarray removeAllObjects];
        }
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            ALPEditorDetailModel *model = [[ALPEditorDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allarray addObject:model];
        }
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}




















/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
